package com.cyzy.controller;

import com.cyzy.entity.RequestDTO;
import com.cyzy.service.PersonalService;
import com.cyzy.util.OrcUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;
@CrossOrigin
@RestController
@RequestMapping("/personal")
public class PersonalController {

    @PostMapping("/idCard")
    public String id(){
        return "success";
    }

    @Value("${uploadPath}")
    private String uploadPath;

    @Autowired
    private PersonalService personalService;

    @PostMapping("/updateRealName")
    public RequestDTO updateRealName(@RequestBody MultipartFile file, String userId) throws Exception {

        File uploadPathFile = new File(uploadPath);
        if (!uploadPathFile.exists()){

            uploadPathFile.mkdirs();
        }

        String uuid = UUID.randomUUID().toString().replace("-", "");

        String[] split = file.getOriginalFilename().split("\\.");

        String newFileNAme = uuid+"."+split[split.length-1];

        String NewFilePath = uploadPath+"\\"+newFileNAme;

        try {
            file.transferTo(new File(NewFilePath));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        System.out.println(NewFilePath);
        int i = personalService.updateRealName(NewFilePath, userId);
        if (i == 1){
            return new RequestDTO(200, "成功", null);
        }
        return new RequestDTO(500, "失败", null);
    }
}
